﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Linq;

namespace MvcApplication1.Controllers
{
    public class TreeExample2Controller : Controller
    {
        public ActionResult Index()
        {
            return View(new TreeExample2Arg(treeItems));
        }

        public ActionResult TreeChildren(string id_te009)
        {
            var items = new List<Tuple<TreeItem, TreeItem>>();
            ToList(treeItems, items, null);
            return PartialView(
                "TreeChildren",
                new TreeChildrenArg(items.Where(_ => _.Item2 != null && _.Item2.GetId() == id_te009).Select(_ => _.Item1)));
        }

        private static void ToList(IEnumerable<TreeItem> items, List<Tuple<TreeItem, TreeItem>> list, TreeItem parent)
        {
            foreach (var item in items)
            {
                list.Add(Tuple.Create(item, parent));
                ToList(item.TreeItems, list, item);
            }
        }


        private static readonly TreeItem[] treeItems = new[]
        {
            new TreeItem(
                NextId,
                new TreeItem(
                    NextId,
                    new TreeItem(NextId),
                    new TreeItem(NextId, true)
                    ),
                new TreeItem(
                    NextId,
                    new TreeItem(NextId),
                    new TreeItem(
                        NextId,
                        new TreeItem(NextId, true),
                        new TreeItem(NextId)
                        ),
                    new TreeItem(NextId),
                    new TreeItem(NextId)
                    ),
                new TreeItem(
                    NextId,
                    new TreeItem(NextId)
                    )
                ),
            new TreeItem(
                NextId,
                new TreeItem(NextId),
                new TreeItem(NextId)
                )
        };

        private static int i;

        private static int NextId
        {
            get { return i++; }
        }
    }

    public class TreeChildrenArg
    {
        public readonly IEnumerable<TreeItem> Items;
        public TreeChildrenArg(IEnumerable<TreeItem> items)
        {
            Items = items;
        }
    }

    public class TreeExample2Arg
    {
        public readonly IEnumerable<TreeItem> Items;
        public TreeExample2Arg(IEnumerable<TreeItem> items)
        {
            Items = items;
        }
    }

    public class TreeItem
    {
        public readonly int Id;
        public readonly TreeItem[] TreeItems;
        public readonly bool IsLeaf;

        public string Title
        {
            get { return "item" + Id; }
        }

        public TreeItem(int id, params TreeItem[] treeItems)
            : this(id, false, treeItems)
        {

        }

        public TreeItem(int id, bool isLeaf, params TreeItem[] treeItems)
        {
            Id = id;
            TreeItems = treeItems;
            IsLeaf = isLeaf;
        }

        public string GetId()
        {
            return Id.ToString();
        }
    }
}